package com.shd.edanpei.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.shd.edanpei.R;
import com.shd.edanpei.base.BaseQuickImageAdapter;
import com.shd.edanpei.base.network.api.ApiCreator;
import com.shd.edanpei.base.network.rx.BaseObserver;
import com.shd.edanpei.base.network.rx.RxTransformer;
import com.shd.edanpei.bean.OrderNumberList;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * Created by mc on 2018/12/18.
 * yang-wen-wen
 */

public class MySpinner extends LinearLayout {

    private View root;
    private TextView tvNumber;
    private ImageView ivShow;
    private RecyclerView recyclerView;
    private Adapter mAdapter;
    private Listener mListener;

    public MySpinner(Context context) {
        super(context);
        initView();
    }

    public MySpinner(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public MySpinner(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        inflate(getContext(), R.layout.view_spinner, this);

        root = findViewById(R.id.ll_root);
        tvNumber = findViewById(R.id.tv_number);
        ivShow = findViewById(R.id.iv_show);
        recyclerView = findViewById(R.id.recycler_view);

        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter = new Adapter(getContext());
        recyclerView.setAdapter(mAdapter);

        initListener();

        getData();
    }

    private PopupWindow window;

    private void initListener() {
        ivShow.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (recyclerView.getVisibility() == VISIBLE) {
                    recyclerView.setVisibility(GONE);
                } else {
                    recyclerView.setVisibility(VISIBLE);
                }
            }
        });
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (mListener != null) {
                    String s = mAdapter.getData().get(position);
                    mListener.select(s);
                    tvNumber.setText(s);
                    recyclerView.setVisibility(GONE);
                }
            }
        });
    }


    private void getData() {
        ApiCreator.getApi().COrderList()
                .compose(RxTransformer.<OrderNumberList>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<OrderNumberList>() {
                    @Override
                    protected void onSuccess(OrderNumberList data) {
                        if (data != null && data.getList().size() > 0) {
                            mAdapter.setNewData(data.getList());
                            if (mListener != null) {
                                String s = mAdapter.getData().get(0);
                                mListener.select(s);
                                tvNumber.setText(s);
                            }
                        }
                    }
                });
    }

    private class Adapter extends BaseQuickImageAdapter<String> {


        public Adapter(Context context) {
            super(context, R.layout.item_myspinner);
        }

        @Override
        protected void convert(BaseViewHolder helper, String item) {

            helper.setText(R.id.tv_number, item);
        }
    }

    public void setSelectListener(Listener listener) {
        mListener = listener;
    }

    public interface Listener {

        void select(String str);
    }

}
